package cn.edu.ctbu.datajpa1.service;


import cn.edu.ctbu.datajpa1.domain.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class StudentServiceTest {


    @Autowired
    private  StudentService studentService;
    @Test
    void findAll() {
        List<Student> students=studentService.findAll();

        assertNotNull(students);
    }

    @Test
    void findById(){
        Student students=studentService.findById(1);

        assertNotNull(students);
    }

    @Test
    void findByNameLike() {
        List<Student> students=studentService.findByName("黎%");
        assertNotNull(students);
    }

    @Test
    void findByNameAndPassword() {
        List<Student> students=studentService.findByNameAndPassword("小影","123456");
        assertNotNull(students);
    }

    @Test
    void insert(){
        Student student=new Student();
        student.setName("张三");
        student.setPassword("666666");
        student.setAge(18);
        student.setScore(77);
        student.setSex(1);
        studentService.insert(student);
        assertNotNull(student.getId());
    }

    @Test
    void update(){
        Student student=studentService.findById(5);
        student.setName("张三1");
        studentService.update(student);

        /*写法二
        List<Student> students=studentService.findByName("张三%");
        Student student=students.get(0);
        student.setName("张三1");
        studentService.update(student);*/

        assertEquals(student.getName(),"张三1");
    }

    @Test
    void delete(){
        studentService.delete(5);
        Student student=studentService.findById(5);
        assertNull(student);
    }


    @Test
    public void filterByName(){
        Student student=new Student();
        student.setName("小");
        Sort sort=Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable= PageRequest.of(0,10,sort);

        //全部模糊查询即 %{name}%
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

        Example<Student> example=Example.of(student,matcher);
        Page<Student> studentPage=studentService.findAll(example,pageable);
        System.out.println(studentPage);
    }


}